﻿using UnityEngine;
using System.Collections;

public class EyeController : MonoBehaviour {

	/*public GameObject leftUpper;
	public GameObject leftLower;
	public GameObject rightUpper;
	public GameObject rightLower;
	public float upperLidDistance;
	public float lowerLidDistance;
	public float blinkTimeIn;
	public float blinkTimeOut;*/
	public float blinkInterval;
	public float blinkIntervalMin;
	public float blinkIntervalMax;
	public float blinkStartTime;
	public float blinkDuration;
	public bool isBlinking = true;
	public bool isFeeding = false;
	public bool startFeeding = false;

	public GameObject leftEye;
	public GameObject rightEye;

	public Texture textureEyeOpenLeft;
	public Texture textureEyeOpenRight;
	public Texture textureEyeClosedLeft;
	public Texture textureEyeClosedRight;

	public GameObject babyFace;

	public Texture textureFaceDefault;
	public Texture textureFaceEating;

	public float feedDurationA;
	public float feedDurationB;

	MoodController moodController;

	// Use this for initialization
	void Start () {

		moodController = Camera.main.GetComponent<MoodController>();	
		moodController.AddMoodChangedCallback(ChangeMood);

		openEyes();

		blinkInterval = Random.Range(blinkIntervalMin, blinkIntervalMax);
		blinkStartTime = Time.time;

	}

	protected void ChangeMood( MoodController.MoodState mood)
	{
		Debug.Log("mood changed in eyeController: " + mood);
		// change audio
		switch(mood)
		{
		case MoodController.MoodState.EATING:
			isBlinking = false;
			isFeeding = true;
			closeEyes();
			eatingFace();
			break;
			
		case MoodController.MoodState.HAPPY:
			openEyes();
			normalFace();
			isBlinking = true;
			isFeeding = false;
			break;
			
		case MoodController.MoodState.RELAXED:
			openEyes();
			normalFace();
			isBlinking = true;
			isFeeding = false;
			break;
			
		default:
			openEyes();
			normalFace();
			isBlinking = true;
			isFeeding = false;
			break;
		}
	}
	
	// Update is called once per frame
	void Update () {

		if (isBlinking && (blinkStartTime + blinkInterval < Time.time))
		{
			StartCoroutine (DoBlinking ());
		}

		if (isFeeding && !startFeeding){
			StartCoroutine (DoFeeding ());
		}
	}

	public IEnumerator DoBlinking ()
	{
		closeEyes();
		yield return new WaitForSeconds (blinkDuration);
		openEyes();

		blinkInterval = Random.Range(blinkIntervalMin, blinkIntervalMax);
		blinkStartTime = Time.time;

		yield return null;
	}

	public IEnumerator DoFeeding ()
	{
		startFeeding = true;
		eatingFace();
		yield return new WaitForSeconds (feedDurationA);
		normalFace();
		yield return new WaitForSeconds (feedDurationB);
		
		startFeeding = false;

		yield return null;
	}

	public void openEyes ()
	{
		leftEye.renderer.material.mainTexture = textureEyeOpenLeft;
		rightEye.renderer.material.mainTexture = textureEyeOpenRight;
	}

	public void closeEyes ()
	{
		leftEye.renderer.material.mainTexture = textureEyeClosedLeft;
		rightEye.renderer.material.mainTexture = textureEyeClosedRight;
	}

	public void normalFace()
	{
		babyFace.renderer.material.mainTexture = textureFaceDefault;
	}

	public void eatingFace()
	{
		babyFace.renderer.material.mainTexture = textureFaceEating;
	}
}
